package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AdminDaliy;
import com.ruoyi.system.domain.AdminDaliyTask;
import com.ruoyi.system.mapper.AdminDaliyMapper;
import com.ruoyi.system.service.IAdminDaliyService;
import com.ruoyi.system.service.IAdminDaliyTaskService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.vo.AdminDaliyView;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 日报Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-12-31
 */
@Service
public class AdminDaliyServiceImpl implements IAdminDaliyService
{
    @Autowired
    private AdminDaliyMapper adminDaliyMapper;

    @Autowired
    private IAdminDaliyTaskService adminDaliyTaskService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询日报
     * 
     * @param daliyId 日报ID
     * @return 日报
     */
    @Override
    public AdminDaliy selectAdminDaliyById(String daliyId)
    {
        AdminDaliy daliy = adminDaliyMapper.selectAdminDaliyById(daliyId);
        AdminDaliyTask task = new AdminDaliyTask();
        task.setDaliyId(daliy.getDaliyId());
        List<AdminDaliyTask> list = adminDaliyTaskService.selectAdminDaliyTaskList(task);
        if (CollectionUtils.isNotEmpty(list)) {
            daliy.setDaliyTask(list);
        }
        return daliy;
    }

    /**
     * 查询日报列表
     * 
     * @param vo 日报
     * @return 日报
     */
    @Override
    public List<AdminDaliyView> selectAdminDaliyListStatistics(AdminDaliyView vo)
    {
        if (null == vo.getUserId() && null == vo.getDeptId()) {
        } else {
            SysUser user = new SysUser();
            user.setDeptId(vo.getDeptId());
            user.setUserId(vo.getUserId());
            List<SysUser> userList = sysUserService.selectUserList(user);
            List<Long> userIds = new ArrayList<Long>();
            userIds.add(Long.valueOf(0));
            if (null != userList) {
                userList.stream().forEach(u ->{
                    userIds.add(u.getUserId());
                });
            }
            vo.setUserIds(userIds);
        }
        List<AdminDaliyView> list = adminDaliyMapper.selectAdminDaliyStatisticsList(vo);
        return list;
    }
/**
     * 查询日报列表
     *
     * @param vo 日报
     * @return 日报
     */
    @Override
    @DataScope(deptAlias = "e", userAlias = "b")
    public List<AdminDaliyView> selectAdminDaliyList(AdminDaliyView vo)
    {
        if (null == vo.getUserId() && null == vo.getDeptId()) {
        } else {
            SysUser user = new SysUser();
            user.setDeptId(vo.getDeptId());
            user.setUserId(vo.getUserId());
            List<SysUser> userList = sysUserService.selectUserList(user);
            List<Long> userIds = new ArrayList<Long>();
            userIds.add(Long.valueOf(0));
            if (null != userList) {
                userList.stream().forEach(u ->{
                    userIds.add(u.getUserId());
                });
            }
            vo.setUserIds(userIds);
        }
        List<AdminDaliyView> list = adminDaliyMapper.selectAdminDaliyListByView(vo);
        return list;
    }

    /**
     * 新增日报
     * 
     * @param adminDaliy 日报
     * @return 结果
     */
    @Override
    public String insertAdminDaliy(AdminDaliy adminDaliy)
    {
        String message = null;
        Long userId = ShiroUtils.getUserId();
        adminDaliy.setUserId(userId);
        List<AdminDaliyView> list = adminDaliyMapper.selectAdminDaliyList(adminDaliy);
        if (CollectionUtils.isNotEmpty(list)) {
            message = String.format("日报日期重复 %s 请重新选择日期", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, adminDaliy.getDaliyDate()));
            return message;
        }
        adminDaliy.setCreateTime(DateUtils.getNowDate());
        adminDaliyMapper.insertAdminDaliy(adminDaliy);
        list = adminDaliyMapper.selectAdminDaliyList(adminDaliy);
        if (CollectionUtils.isNotEmpty(list) && list.size() != 1) {
            message = String.format("日报插入数量不等于1 daliydate:%s,userid:%s,size:%s",adminDaliy.getDaliyDate(), userId, list.size());
            return message;
        }
        Long daliyId = list.get(0).getDaliyId();
        for (AdminDaliyTask task: adminDaliy.getDaliyTask()) {
            if (StringUtils.isEmpty(task.getTaskContent())) {
                continue;
            }
            task.setUserId(userId);
            task.setDaliyId(daliyId);
            adminDaliyTaskService.insertAdminDaliyTask(task);
        }
        return null;
    }

    /**
     * 修改日报
     * 
     * @param adminDaliy 日报
     * @return 结果
     */
    @Override
    public int updateAdminDaliy(AdminDaliy adminDaliy)
    {
        for (AdminDaliyTask task: adminDaliy.getDaliyTask()) {
            if (StringUtils.isEmpty(task.getTaskContent())) {
                continue;
            }
            if (null != task.getTaskId() && 0 != task.getTaskId()) {
                adminDaliyTaskService.updateAdminDaliyTask(task);
            }else {
                task.setDaliyId(adminDaliy.getDaliyId());
                adminDaliyTaskService.insertAdminDaliyTask(task);
            }
        }
        return adminDaliyMapper.updateAdminDaliy(adminDaliy);
    }

    /**
     * 删除日报对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAdminDaliyByIds(String ids)
    {
        adminDaliyTaskService.deleteAdminDaliyTaskByDaliyIds(ids);
        return adminDaliyMapper.deleteAdminDaliyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除日报信息
     * 
     * @param daliyId 日报ID
     * @return 结果
     */
    @Override
    public int deleteAdminDaliyById(Long daliyId)
    {
        return adminDaliyMapper.deleteAdminDaliyById(daliyId);
    }
}
